import datetime

from starlette.background import BackgroundTasks

from items.article import Article, ContextDesc
from items.response import GenericResponse
from service.article_service import ArticleService


def get_article(
        article_id: int

):
    """ 获取文章 """
    article_service = ArticleService()
    article = article_service.get_article(article_id)
    return GenericResponse(
        now=int(datetime.datetime.now().timestamp()),
        data={
            "article": article
        }
    )


def create_article(
        article_info: Article,
        background_tasks: BackgroundTasks

):
    """ 创建文章 """

    article_service = ArticleService()
    article_id = article_service.create_article(**article_info.dict(), background_tasks=background_tasks)
    return GenericResponse(
        now=int(datetime.datetime.now().timestamp()),
        data={
            "article_id": article_id
        }
    )


def get_context(
        article_id: int

):
    """ 获取上下文 """
    article_service = ArticleService()
    context = article_service.get_context(article_id)
    return GenericResponse(
        now=int(datetime.datetime.now().timestamp()),
        data={
            "context": context
        }
    )


def create_context(
        desc: ContextDesc,
        background_tasks: BackgroundTasks
):
    """
    创建上下文
    :param desc: 对上下文的描述
    :param background_tasks: 后台任务
    :return: 文章id
    """
    article_service = ArticleService()
    article_id = article_service.create_context(**desc.dict(), background_tasks=background_tasks)
    return GenericResponse(
        now=int(datetime.datetime.now().timestamp()),
        data={
            "article_id": article_id
        }
    )
